package com.ict.web.controller.yzq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ict.common.constant.Constants;
import com.ict.common.core.controller.BaseController;
import com.ict.common.core.domain.AjaxResult;
import com.ict.common.core.redis.RedisCache;
import com.ict.common.exception.user.CaptchaException;
import com.ict.common.exception.user.CaptchaExpireException;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.system.service.ISysConfigService;
import com.ict.web.domain.ExpertInformationSheet;
import com.ict.web.mapper.ReviewTaskMapper;
import com.ict.web.service.IExpertInformationSheetService;
import com.ict.web.service.IReviewTaskService;
import com.ict.web.vo.ReviewTaskDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author xc
 */
@Api(tags = "专家接受任务H5")
@RestController
@RequestMapping("/h5")
public class ExpertDealWithController extends BaseController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IReviewTaskService reviewTaskService;
    @Resource
    private IExpertInformationSheetService expertInformationSheetService;
    @Autowired
    private ReviewTaskMapper reviewTaskMapper;
    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    @ApiOperation(value ="生成验证码")
    public AjaxResult getCode(){
        AjaxResult ajax = AjaxResult.success();
        boolean captchaOnOff = configService.selectCaptchaOnOff();
        ajax.put("captchaOnOff", captchaOnOff);
        if (!captchaOnOff) {
            return ajax;
        }
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String code = null;
        // 生成验证码
        code = getCheckCode();
        redisCache.setCacheObject(verifyKey, code);
        ajax.put("uuid", uuid);
        ajax.put("code", code);
        return ajax;
    }
    /**
     * 校验验证码
     *
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    @GetMapping("/validateCaptcha")
    @ApiOperation(value ="校验验证码")
    public AjaxResult validateCaptcha(String code, String uuid)
    {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        if (captcha == null)
        {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha))
        {
            throw new CaptchaException();
        }
        return AjaxResult.success();
    }
    /**
     * 专家处理操作前的详情信息
     */
    @GetMapping("/detailByTask/{reviewTaskId}")
    @ApiOperation(value ="查看评审任务详情（任务id）")
    public ReviewTaskDetailVo detailByTaskId(@PathVariable Long reviewTaskId)
    {
        return reviewTaskService.reviewTaskDetailByTaskId(reviewTaskId);
    }
    /**
     * 专家处理操作后的详情信息
     *
     */
    @GetMapping("/expertReceiveInfo/{reviewTaskId}")
    @ApiOperation(value = "专家处理操作后的详情信息")
    public AjaxResult getExpertReceiveInfo(@PathVariable("reviewTaskId") Long reviewTaskId,Long userId) {
        QueryWrapper<ExpertInformationSheet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("del_flag",ExpertInformationSheet.DELETE_STATUS_NORMAL);
        List<ExpertInformationSheet> expertInformationSheetList = expertInformationSheetService.list(queryWrapper);
        Assert.isTrue(expertInformationSheetList.size()!=0,"该用户未绑定专家");
        Assert.isTrue(expertInformationSheetList.size()==1,"该用户绑定了多个专家，无法处理该评审任务");
        ExpertInformationSheet expertInfo = expertInformationSheetList.get(0);
        return AjaxResult.success(reviewTaskMapper.selectAcceptDetail(reviewTaskId,expertInfo.getExpertId()));
    }
    /**
     * 专家接受任务操作H5
     */
    @PutMapping("/dealWith")
    @ApiOperation(value = "专家接受任务操作H5")
    public AjaxResult dealWithH5(Long reviewTaskId, String receiveStatus, String refuseReason, Long userId){
        return toAjax(reviewTaskService.dealWithH5(reviewTaskId,receiveStatus,refuseReason,userId));
    }

    /**
     * 随机四位验证码
     * @return
     */
    public String getCheckCode() {
        String ZiMu = "1234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(ZiMu.length());
            char c = ZiMu.charAt(index);
            result += c;
        }
        return result;

    }
}
